<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    <%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" type="text/css" href="<s:url value='/resources/css/shop/addGoods.css'/>" media="all" />
<script type="text/javascript" src="<s:url value='/resources/js/jquery/jquery-1.5.2.min.js'/>"></script> 
<style type="text/css">
	
	
</style>
<script type="text/javascript">
	var contextpath= "<%=request.getContextPath()%>";
	var goodsindex=1;
	function addGoods(goodsid){
		
		//如果已有该商品则数量加1
		var goods=$("#goods"+goodsid);
		if(goods.size()>0){
			var tr=goods;
			var index=tr.find("input.num");
			var numStr=index.val();
			var num=parseInt(numStr);
			num++;
			
			var totalStr=$(".total>span").text();
			var total=parseFloat(totalStr);
			var price=tr.find(".price").text();
			total+=parseFloat(price);
			total=total.toFixed(2);
			$(".total>span").text(total);
			
			index.val(num);
		}else{
			$.ajax({
				type: "POST",
				url: "<s:url value="/shop/queryGoodsInfo.do" />",
				data: {"goodsid":goodsid},
				success: function(msg){
					if(typeof(msg) == 'string'){
						msg = eval("("+msg+")");
					}
					if(msg.returnCode==0){
						var newgoods=msg.messageObj;
						addGoodsInfo(newgoods);
					}
				}
			}); 
		}
		
		/* goodsindex++;
		goodsindex=goodsindex%5;//用于模拟 */
	
	}
	
	
	function addGoodsInfo(goods){
		if(typeof(goods) == 'string'){
			goods = eval("("+goods+")");
		}
		if(goods==null){
			return null;
		}
		
		
		var html='';
		html+='<tr class="good" id="goods'+goods.objid+'">';
		html+='	<td>';
		html+='		<div class="litbt del"></div>'+goods.name+'';
		html+='		<input type="hidden" value="'+goods.objid+'" name="shoppingList[0].goodsid" size="2">';
		html+='	</td>';
		html+='	<td class="price">'+goods.price.toFixed(2)+'</td>';
		html+='	<td>';
		html+='		<div class="litbt lose"></div>';
		html+='		<input type="text" class="num" readonly="readonly" value="1" name="shoppingList[0].num" style="width:24px;float:left;">';
		html+='		<div class="litbt add"></div>';
		html+='	</td>';
		html+='</tr>';
		$("#goods").append(html);
		$(".total").css("padding-top","37px");
		var tableh=$("#goodstable").height();
		var lineh=tableh-38;
		$(".total").css("line-height",lineh+"px");
		var totalStr=$(".total>span").text();
		var total=parseFloat(totalStr);
		total+=parseFloat(goods.price);
		total=total.toFixed(2);
		$(".total>span").text(total);
		
		changeOrder(".good");
	}
	
	$(function(){
		$(".del").live("click",function(){
			var tr=$(this).parents("tr");
			
			//原总价
			var totalStr=$(".total>span").text();
			var total=parseFloat(totalStr);
			var priceStr=tr.find(".price").text();
			var price=parseFloat(priceStr);
			//新总价
			var numStr=tr.find("input.num").val();
			var num=parseInt(numStr);
			var sum=price*num;
			total-=sum;
			total=total.toFixed(2);
			$(".total>span").text(total);
			
			//删除并调高
			tr.remove();
			$(".total").css("padding-top","37px");
			var tableh=$("#goodstable").height();
			var lineh=tableh-38;
			$(".total").css("line-height",lineh+"px");
			if(lineh==0){
				$(".total").css("padding-top","0px");
				$(".total").css("line-height","37px");
			}
			if(lineh==1){
				$(".total").css("padding-top","0px");
				$(".total").css("line-height","37px");
			}
			
			changeOrder(".good");
		});
		
		$(".lose").live("click",function(){
			var tr=$(this).parents("tr");
			
			var index=$(this).parents("tr").find("input.num");
			var numStr=index.val();
			var num=parseInt(numStr);
			num--;
			if(num>=1){
				index.val(num);
				
				var totalStr=$(".total>span").text();
				var total=parseFloat(totalStr);
				var price=tr.find(".price").text();
				total-=parseFloat(price);
				total=total.toFixed(2);
				$(".total>span").text(total);
			}
		});
				
		$(".add").live("click",function(){
			var tr=$(this).parents("tr");
			var index=$(this).parents("tr").find("input.num");
			var numStr=index.val();
			var num=parseInt(numStr);
			num++;
			
			
			var totalStr=$(".total>span").text();
			var total=parseFloat(totalStr);
			var price=tr.find(".price").text();
			total+=parseFloat(price);
			total=total.toFixed(2);
			$(".total>span").text(total);
			
			index.val(num);
		});
		
		//二维码扫描添加商品
		
		//初始行程id输入框,设置为焦点
		$("#id_input").focus();
		
		//5秒检测一次input是否为焦点
		time=setInterval(function(){
			$("#id_input").focus();
		}, 5*1000);
		//二维码扫描结果的回车键
		$("#id_input").bind('keydown', function (e) {
            var key = e.which;
 
            if (key == 13) {
            	var goodsid=$(this).val();
            	addGoods(goodsid); 
            	$(this).val("");
            	//不再返回给浏览器
            	return false;
            }
        });
		
		
		//取消
		$(".cancel").click(function(){
			window.parent.location.href=contextpath+"/shop/intoShop.do?placeid="+<s:property value="guideSign.placeid" />;
		});
		
		$("#payForm").submit(function(){
			var goods=$("#goods>tr");
			if(goods.size()>0){
				return true;
			}else{
				return false;
			}
		});
	});
	
	//修改list顺序序号
	function changeOrder(listname){
		var list=$(listname);
		for(var i=0;i<list.size();i++){
			var driver=$(list[i]);
			var inputs=driver.find("input");
			for(var j=0;j<inputs.size();j++){
				var input=$(inputs[j]);
				var name=input.attr("name");
				if(name!=null){
					name=name.replace(/\[([\s\S]+?)\]/i,"["+i+"]");
					input.attr("name",name);
				}
			}
			
			var textareas=driver.find("textarea");
			for(var j=0;j<textareas.size();j++){
				var textarea=$(textareas[j]);
				var name=textarea.attr("name");
				if(name!=null){
					name=name.replace(/\[([\s\S]+?)\]/i,"["+i+"]");
					textarea.attr("name",name);
				}
			}
		}
	}
</script>
</head>
<body>
<div class="content">
	<div class="headText">旅行团信息</div>
	<form id="payForm" action="<s:url value="/shop/pay.do" />" method="post">
		<input type="hidden" name="order.planid" value="<s:property value="guideSign.planid" />">
		<input type="hidden" name="order.placeid" value="<s:property value="guideSign.placeid" />">
	<div >
		<div style="float: left;">
			<table id="goodstable">
				<thead>
					<tr>
						<td>商品名称</td>
						<td width="90px">单价</td>
						<td width="125px">数量</td>
					</tr>
				</thead>
				<tbody id="goods">
					
				</tbody>
			</table>
		</div>
		<div class="total" style="">
			总价：
			<span>00.00</span>
		</div>
		<div class="clr"></div>
	</div>
	<div class="menu">
		<div class="moni" style="display: none;">
			<button class="bt monibt" type="button" onclick="addGoods(113030014033)"></button>
		</div>
		<div class="cancel">
			<button class="bt cancelbt" type="reset"></button>
		</div>
		<div  class="pay">
			<button class="bt paybt" type="submit"></button>
		</div>
		<div class="clr"></div>
	</div>
	</form>
	<div class="id_input_case">
		<input id="id_input" type="text" class="" value="">
	</div>
</div>
</body>
</html>